﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Maintenance
{
    public partial class FrmScreenProtect : Form
    {
        //程序启动路径，与生成程序的exe文件在同一目录下
        public String exePath = Application.StartupPath;

        private string imageUrl = PublicGlobal.appConfigure.GetConfigureValue("Maintenance", "imageUrl", "http://192.168.0.3:8888/");

        private int imageIndex = 0;

        //窗口左上角位置
        public Point m_formAutographShowPos;

        public MiniForm miniForm;

        public FrmScreenProtect(MiniForm _miniForm, int rotationTime)
        {
            InitializeComponent();
            miniForm = _miniForm;
            //设置窗体尺寸
            this.Width = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
            this.ClientSize = new System.Drawing.Size(Width, Height);
            //计算初始位置
            m_formAutographShowPos.X = 0;
            m_formAutographShowPos.Y = 0;
            Location = m_formAutographShowPos;
            this.timeChangeBackground.Interval = rotationTime;
            //窗体背景图
            this.background();
        }

        private void FrmScreenProtect_MouseMove(object sender, MouseEventArgs e)
        {
        }

        private void FrmScreenProtect_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.timer1.Enabled = true;
            this.timeChangeBackground.Enabled = true;
            refreshTime();
            this.TopMost = true;
            this.BringToFront();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            refreshTime();
        }

        private void refreshTime()
        {
            lbl_time_hour.Text = DateTime.Now.ToString("HH:mm");
            lbl_time_week.Text = DateTime.Now.ToString("yyyy年MM月dd日 dddd");
        }

        private void changeBackGroundImage(String imageName)
        {
            string imagePath = imageUrl + imageName;
            Image imgBack = null;
            try
            {
                imgBack =
                    Image.FromStream(WebRequest.Create(imagePath).GetResponse().GetResponseStream());
                if (imgBack == null)
                {
                    Random rd = new Random();
                    DirectoryInfo folder = new DirectoryInfo(exePath + "\\bgimage");
                    var files = folder.GetFiles("*.jpg");
                    imgBack = Image.FromFile(exePath + "\\bgimage\\" + files[rd.Next(0, files.Length - 1)].Name);
                }
            }
            catch (Exception ex)
            {
                PublicGlobal.logger.Error("获取网络图片失败," + ex.Message);
                Random rd = new Random();
                DirectoryInfo folder = new DirectoryInfo(exePath + "\\bgimage");
                var files = folder.GetFiles("*.jpg");
                imgBack = Image.FromFile(exePath + "\\bgimage\\" + files[rd.Next(0, files.Length - 1)].Name);
            }
            this.BackgroundImage = imgBack;
        }

        private void timeChangeBackground_Tick(object sender, EventArgs e)
        {
            this.background();
        }

        private void background()
        {
            try
            {
                string imagedata = imageUrl + "imagedata.json";
                WebClient webpage = new WebClient();
                var imageJson = webpage.DownloadString(imagedata);
                JObject json = (JObject)JsonConvert.DeserializeObject(imageJson);
                JArray array = (JArray)json["images"];
                int num = array.Count;
                imageIndex++;
                if (imageIndex > num - 1)
                {
                    imageIndex = 0;
                }
                changeBackGroundImage(array[imageIndex].ToString());
            }
            catch (Exception ex)
            {
                PublicGlobal.logger.Error("获取图片配置失败," + ex.Message);
            };
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            PublicGlobal.HideScreenProtect();
        }
    }
}